package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.cms.entity.CmsSubject;
import com.itheima.common.domain.AjaxResult;
import com.itheima.common.domain.PageInfo;
import com.itheima.service.ICmsSubjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/subject")
public class CmsSubjectController {
    @Autowired
    private ICmsSubjectService cmsSubjectService;

    /**
     * 根据专题名称分页获取商品专题
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "keyword", required = false) String title,
                           @RequestParam("pageNum") Integer pageNum,
                           @RequestParam("pageSize") Integer pageSize) {
        Page<CmsSubject> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<CmsSubject> lqw = new LambdaQueryWrapper();
        lqw.like(title != null, CmsSubject::getTitle, title);
        cmsSubjectService.page(page, lqw);

        PageInfo pageInfo = PageInfo.toPageList(page);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 获取全部商品专题
     *
     * @return
     */
    @GetMapping("/listAll")
    public AjaxResult listAll() {
        List<CmsSubject> list = cmsSubjectService.list();
        return AjaxResult.success(list);
    }
}
